package com.dogkr.server.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dogkr.common.Constants;
import com.dogkr.server.entity.Host;
import com.dogkr.server.entity.PetServer;
import com.dogkr.server.entity.PetServerRestrain;
import com.dogkr.server.entity.PetServerValuation;
import com.dogkr.server.mapper.HostMapper;
import com.dogkr.server.mapper.HostPetServerMapper;
import com.dogkr.server.mapper.HostPetServerRestrainMapper;
import com.dogkr.server.mapper.HostPetServerValuationMapper;
import com.dogkr.server.mapper.SysPetServerMapper;
import com.dogkr.server.mapper.SysPetServerRestrainMapper;
import com.dogkr.server.mapper.SysPetServerValuationMapper;
import com.sean.common.service.BaseService;

@Service
public class PetServerService extends BaseService{
	@Autowired
	private SysPetServerMapper sysPetServerMapper;
	@Autowired
	private SysPetServerValuationMapper sysPetServerValuationMapper;
	@Autowired
	private SysPetServerRestrainMapper sysPetServerRestrainMapper;
	
	@Autowired
	private HostPetServerMapper hostPetServerMapper;
	@Autowired
	private HostPetServerValuationMapper hostPetServerValuationMapper;
	@Autowired
	private HostPetServerRestrainMapper hostPetServerRestrainMapper;
	
	@Autowired
	private HostMapper hostMapper;
	
	public void saveHostServerWithAll(List<PetServer> serverList, Long hostId) {
		this.deleteHostServerByHostIdWithAll(hostId);
		for (PetServer petServer : serverList) {
			petServer.setHostId(hostId);
			petServer.setLastModifydate(new Date());
			petServer.setStatus(Constants.STATUS_NORMAL);
			hostPetServerMapper.insert(petServer);
			for (PetServerValuation valuation : petServer.getPetServerValuationList()) {
				valuation.setServerId(petServer.getId());
				valuation.setStatus(Constants.STATUS_NORMAL);
				hostPetServerValuationMapper.insert(valuation);
				//设置店铺基础服务价格
				if(PetServer.BASE_SERVER_CODE.equalsIgnoreCase(petServer.getServerCode())) {
					Host host = new Host();
					host.setId(hostId);
					host.setServerVal(valuation.getValueVal());
					hostMapper.updateByPrimaryKeySelective(host);
				}
			}
			if(petServer.getPetServerRestrainList() != null) {
				for (PetServerRestrain restrain : petServer.getPetServerRestrainList()) {
					restrain.setServerId(petServer.getId());
					restrain.setStatus(Constants.STATUS_NORMAL);
					hostPetServerRestrainMapper.insert(restrain);
				}
			}
		}
	}
	
	public void deleteHostServerByHostIdWithAll(Long hostId) {
		List<PetServer> serverList = hostPetServerMapper.selectByHostKey(hostId);
		for (PetServer petServer : serverList) {
			hostPetServerValuationMapper.deleteByServerKey(petServer.getId());
			hostPetServerRestrainMapper.deleteByServerKey(petServer.getId());
		}
		hostPetServerMapper.deleteByHostKey(hostId);
	}
	
	public List<PetServer> selectSysPetServerWithAll() {
		return sysPetServerMapper.selectAll();
	}

	/**
	 * 设置保存的服务与系统服务属性一致.
	 * @param serverList
	 */
	public void setSysServerAttr(List<PetServer> serverList) {
		List<PetServer> sysServers = this.selectSysPetServerWithAll();
		for (PetServer hostPetServer : serverList) {
			for (PetServer sysPetServer : sysServers) {
				if(sysPetServer.getServerCode().equalsIgnoreCase(hostPetServer.getServerCode())) {
					this.extendServerWithAll(sysPetServer, hostPetServer);
				}
			}
		}
	}
	
	public void extendServerWithAll(PetServer sysPetServer, PetServer hostPetServer) {
		hostPetServer.setServerName(sysPetServer.getServerName());
		hostPetServer.setValueType(sysPetServer.getValueType());
		for(PetServerValuation valuation : hostPetServer.getPetServerValuationList()) {
			for(PetServerValuation sysValuation : sysPetServer.getPetServerValuationList()) {
				if(sysValuation.getId().longValue() == valuation.getSysId()) {
					valuation.setValueName(sysValuation.getValueName());
					valuation.setValueType(sysValuation.getValueType());
				}
			}
		}
		if(hostPetServer.getPetServerRestrainList() != null) {
			for(PetServerRestrain restrain : hostPetServer.getPetServerRestrainList()) {
				for(PetServerRestrain sysRestrain : sysPetServer.getPetServerRestrainList()) {
					if(sysRestrain.getId().longValue() == restrain.getSysId()) {
						restrain.setRestrainName(sysRestrain.getRestrainName());
						restrain.setRestrainType(sysRestrain.getRestrainType());
					}
				}
			}
		}
	}

	public List<PetServer> selectHostPetServerWithAll(Long hostId) {
		return hostPetServerMapper.selectByHostKey(hostId);
	}
}
